package com.fs.ibox.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fs.ibox.pojo.IdCollection;
import com.fs.ibox.pojo.Orders;
import com.fs.ibox.pojo.User;
import com.fs.ibox.service.CollectionService;
import com.fs.ibox.service.IdCollectionService;
import com.fs.ibox.service.OrdersService;
import com.fs.ibox.service.UserService;
import com.fs.ibox.util.BeanFactory;
import com.fs.ibox.util.ResponseDataUtil;
import com.fs.ibox.util.ResponseJSONUtil;
import com.fs.ibox.vo.ResponseData;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet(name = "BuyServlet", value = "/BuyServlet/*")
public class BuyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        //获取前端传递的json数据
        StringBuffer stringBuffer = new StringBuffer();
        //请求输入流
        BufferedReader reader = request.getReader();
        String line = null;

        while((line = reader.readLine()) != null){
            stringBuffer.append(line);
        }
        //获取json中的数据
        JSONObject js = JSON.parseObject(stringBuffer.toString());
        String path = request.getPathInfo();
        if("/queryUserAll".equals(path)){
            queryUserAll(request,response);
        }
        else if("/reviseByid".equals(path)){
            reviseByid(request,response);
        }
        else if("/addIdcollection".equals(path)){
            addIdcollection(request,response,js);
        }
        else if("/DeductionsByUsername".equals(path)){
            DeductionsByUsername(request,response);
        }
        else if("/addOrders".equals(path)){
            addOrders(request,response,js);
        }
    }

    private void addOrders(HttpServletRequest request, HttpServletResponse response,JSONObject js) throws IOException {
        Orders orders = new Orders();
        String username = js.getString("username");
        String price = js.getString("price");
        double douprice=Double.parseDouble(price);
        String cname = js.getString("cname");
        String cimg = js.getString("cimg");
        orders.setUsernameSell(username);
        orders.setOprice(douprice);
        orders.setCimg(cimg);
        orders.setCname(cname);
        orders.setOrdertime(new Date());
        orders.setOrdersId("#"+System.currentTimeMillis());
        orders.setUsernameBy("官方直售");
        OrdersService bean = BeanFactory.createBean(OrdersService.class);
        bean.addOrders(orders);
        ResponseData mmsg = ResponseDataUtil.buildOk("购买成功");
        ResponseJSONUtil.responseJSON(mmsg,response);



    }

    private void DeductionsByUsername(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String price = request.getParameter("price");
        double douprice=Double.parseDouble(price);
        UserService bean = BeanFactory.createBean(UserService.class);
        bean.DeductionsByUsername(username,douprice);
        ResponseData mssg = ResponseDataUtil.buildOk("扣款成功");
        ResponseJSONUtil.responseJSON(mssg,response);

    }

    private void addIdcollection(HttpServletRequest request, HttpServletResponse response ,JSONObject js) throws IOException {
        IdCollection idCollection = new IdCollection();
        String username = js.getString("username");
//        String username = request.getParameter("username");
//        String cname = request.getParameter("cname");
        String cname = js.getString("cname");
        String price = js.getString("price");
//        String price = request.getParameter("price");
        double douprice=Double.parseDouble(price);
//        String cid = request.getParameter("cid");
        String cid = js.getString("cid");
        idCollection.setCid(""+cid);
        idCollection.setCname(cname);
        idCollection.setUsername(username);
        idCollection.setPrice(douprice);
        IdCollectionService bean = BeanFactory.createBean(IdCollectionService.class);
        bean.addIdcollection(idCollection);
        ResponseData mssg = ResponseDataUtil.buildOk("购买成功");
        ResponseJSONUtil.responseJSON(mssg,response);
    }

    private void reviseByid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int intid=Integer.parseInt(id);
        CollectionService bean = BeanFactory.createBean(CollectionService.class);
        bean.reviseByid(intid);
        ResponseData msgg = ResponseDataUtil.buildOk("购买成功");
        ResponseJSONUtil.responseJSON(msgg,response);

    }

    private void queryUserAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        UserService bean = BeanFactory.createBean(UserService.class);
        List<User> users = bean.queryByusername(username);
        ResponseData responseData = ResponseDataUtil.buildOk(users);
        ResponseJSONUtil.responseJSON(responseData,response);
    }
}
